<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
</body>
<script>
    console.log('-------------------字符串 String------------------------')
    //1.截取字符串"我爱中华人民共和国"，中的"中华"
    let string='我爱中华人民共和国';
    console.log(string.substr(2,2))
    //2."abcoefoxyozzopp"查找字符串中所有o出现的位置
    let s='abcoefoxyozzopp'
    let  os = [];
    for (let i = 0; i < s.length; i++) {
        if (s[i] == "o") {
            os.push(i);
        }
    }
    console.log(os);
    //3.把字符串中所有的o替换成!
    function replace(str) {
        return str.replaceAll('o', '!');
    }
    let original= 'Hello World!';
    let replaced = replace(original);
    console.log(replaced); 

    //4.判断一个字符串中出现次数最多的字符，统计这个次数
    let ss = 'abcoefoxyozzopp';
    let o = {};
    for (let i = 0; i < ss.length; i++){
        let item = ss.charAt(i);
        if(o[item]){
            o[item] ++
        }else{
            o[item] = 1;
        }
    }
    let max = 0;
    let char;
    for(let key in o){
        if(max < o[key]){
            max = o[key]
            char = key
        }
    }
    console.log("出现次数最多的字符为：" + char)
    console.log("次数为：" + max)

    console.log('-------------------数组 Array------------------------')
    // 1.将一个字符串数组输出为|分割的形式，比如“刘备|张飞|关羽”。使用两种方式实现
    let name = ["刘备", "张飞", "关羽"];
    let result1 = name.join("|");
    console.log(result1);

    let result2 = "";
    for (let i = 0; i < name.length; i++) {
        result2 += name[i];
        if (i !== name.length - 1) {
            result2 += "|";
        }
    }
    console.log(result2);

    // 2.将一个字符串数组的元素的顺序进行反转。["a", "b", "c", "d"] -> [ "d","c","b","a"]。使用两种种方式实现。
    let chars1= ["a", "b", "c", "d"];
    chars1 = chars1.reverse(); 
    console.log(chars1);
    
    let chars2 = ["a", "b", "c", "d"];
    let chars3 = [];
    for (let i = chars2.length - 1; i >= 0; i--) {
    chars3.push(chars2[i]);
    }
    console.log(chars3);  

    // 3.工资的数组[1500, 1200, 2000, 2100, 1800],把工资超过2000的删除
    salaries = [1500, 1200, 2000, 2100, 1800];
    for (let i = 0; i < salaries.length; i++) {
        if (salaries[i] > 2000) {
            salaries.splice(i, 1);
            i--; 
        }
    }
    console.log(salaries);

    // 4.["c", "a", "z", "a", "x", "a"]找到数组中每一个a出现的位置
    let c = ["c", "a", "z", "a", "x", "a"];
    let positions= [];
    for (let i = 0; i < c.length; i++) {
        if (c[i] =="a")
        positions.push(i);
    }
    console.log(positions); 
    // 5.编写一个方法去掉一个数组的重复元素
    console.log('----------- 数学 Math ------------')
    //1.求10-20之间的随机数
   console.log(Math.floor(Math.random()*11)+10 )
    //2.随机生成颜色 例如：#3c3c3c
    function getColor(){
        let str = "#"; 
        let arr = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']; 
        for(let i=0;i<6;i++){ 
            let index = parseInt(Math.random()*16) 
            str += arr[index] 
        }
        return str; 
    }
    console.log(getColor()); 

    console.log('--------------------时间  Date----------------------------')
    //1.写一个函数，格式化日期对象，返回yyyy-MM-dd HH:mm:ss的形式
    function formatDate(date) {
        if (!(date instanceof Date)) {
            return;
        }
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let d = date.getDate();
        let hour = date.getHours();
        let minute = date.getMinutes();
        let second = date.getSeconds();
        month = month < 10 ? '0' + month : month;
        d = d < 10 ? '0' + d : d;
        hour = hour < 10 ? '0' + hour : hour;
        minute = minute < 10 ? '0' + minute : minute;
        second = second < 10 ? '0' + second : second;
        return year + '-' + month + '-' + d + ' ' + hour + ':' + minute + ':' + second;
    }
    let str = formatDate(new Date())
    console.log(str)
    //2.计算时间差，返回相差的天/时/分/秒
    function getTimeDifference(startTime, endTime) {
    const dif = endTime - startTime;
    const days = Math.floor(dif / (1000 * 60 * 60 * 24));
    const hours = Math.floor((dif% (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((dif % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((dif % (1000 * 60)) / 1000);
    console.log(`相差 ${days} 天 ${hours} 时 ${minutes} 分 ${seconds} 秒`);
    }
    const start = new Date(2023, 2, 22, 10, 30, 11);
    const end = new Date(2025, 5, 25, 12, 30, 45);
    getTimeDifference(start, end);

    console.log('--------------------正则表达式 RegExp ----------------------------')
    //1.查看字符串中是否包含数字
    console.log(/\d/.test('abc123aaa')); 
    console.log(/\d/.test('aaabbb'));  

    //2.验证中文名，且2到5位
    function checkChinese(str) {
        return /^[\u4e00-\u9fa5]{2,5}$/.test(str); 
    }
    console.log(checkChinese('张三'));    
    console.log(checkChinese('奥斯特洛夫斯基')); 
    console.log(checkChinese('12'));  

    //3.将以下字符串中的数字全部筛选出来  中国移动:10086,中国联通:10010,中国电信:10000  match()
    let str4 = '中国移动:10086,中国联通:10010,中国电信:10000';
    let nums = str4.match(/\d+/g); 
    console.log('提取的数字：', nums); 


    //4.提取年月日的日期中的年份 2022-10-20
    let dateStr = '2022-10-20';
    let reg5 = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
    if(reg5.test(dateStr)){
        console.log('年份：', RegExp.$1); 
    }
    //5.将邮箱的各部分进行提取  123456@qq.com
    let email = '123456@qq.com';
    let reg6 = /^(\w+)@(\w+)\.(\w+)$/;
    if(reg6.test(email)){
        console.log('用户名：', RegExp.$1); 
        console.log('@符号：', RegExp.$2);   
        console.log('域名：', RegExp.$3);    
    }

    //6.'abc   123   aaa   bbb' 将此字符串中的空格替换成AAA  replace()
    let str6 = 'abc 123   aaa  bbb';
    let newStr = str6.replace(/\s+/g, 'AAA'); 
    console.log('替换空格后：', newStr); 

    //7.切割字符串  abc#123@bbb  split()
    let str7 = 'abc#123#bbb';
    let arr7 = str7.split(/[#@]/); 
    console.log('切割结果：', arr7); 
    </script>

</html>